package oa.paging;

import java.util.ArrayList;
import java.util.List;


/**
 * 分页实体类
 * 
 * @author acer
 *
 */
public class Page<T> {

	private int currentPage = 1;// 当前页
	private int pageSize = 0;// 每页显示的行数

	private int totalPage;// 总页数
	private int totalRow;// 总行数
	private int prevPage;// 上一页
	private int nextPage;// 下一页
    private List<T> PageList; //信息集合
    
    
	/**
	 *  页码组
	 */
	private int groupPageCount = 5;// 默认显示5页 1-5
	private List<Integer> groupPage = new ArrayList<Integer>();// 显示组页数
	public List<T> getPageList() {
		return PageList;
	}

	public void setPageList(List<T> pageList) {
		PageList = pageList;
	}
	public Page() {}
	public Page(int currentPage, int pageSize) {
		super();
		this.currentPage = currentPage;
		this.pageSize = pageSize;
	}

	public int getCurrentPage() {
		return currentPage;
	}

	public void setCurrentPage(int currentPage) {
		this.currentPage = currentPage;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalRow() {
		return totalRow;
	}

	/**
	 * 设置总行数
	 * 
	 * @param totalRow
	 */
	public void setTotalRow(int totalRow) {
		this.totalRow = totalRow;

		// 计算总页数
		if (this.totalRow % this.pageSize == 0) {
			this.totalPage = this.totalRow / this.pageSize;
		} else {
			this.totalPage = this.totalRow / this.pageSize + 1;
		}
	}

	/**
	 * 判断是否有上一页
	 * 
	 * @return
	 */
	public boolean getHasPrevPage() {

		return this.currentPage - 1 > 0;
	}

	/**
	 * 上一页
	 * 
	 * @param prevPage
	 */
	public int getPrevPage() {
		if (this.currentPage - 1 > 0) {
			this.prevPage = this.currentPage - 1;
		}
		return prevPage;
	}

	/**
	 * 判断是否有下一页
	 * 
	 * @return
	 */
	public boolean getHasNextPage() {
		return this.currentPage + 1 <= this.totalPage;
	}

	/**
	 * 下一页
	 * 
	 * @return
	 */
	public int getNextPage() {

		if (this.currentPage + 1 <= this.totalPage) {
			this.nextPage = this.currentPage + 1;
		}
		return nextPage;
	}


	public void setPrevPage(int prevPage) {
		this.prevPage = prevPage;
	}

	public void setNextPage(int nextPage) {
		this.nextPage = nextPage;
	}

	
	public int getTotalPage() {
		return totalPage;
	}
	/**
	 * 显示一组的页数
	 * 
	 * @return
	 */
	public List<Integer> getGroupPage() {
		
		//定义起始页 - 结束页
		int startPage=1,endPage=this.groupPageCount;
		
		//在当前页之前几页
		int firstPage = groupPageCount / 2;
		//在当前页之后几页
		int lastPage = groupPageCount - firstPage;
		
		//计算起始页
		if((this.currentPage - firstPage) > 0 && this.totalPage - this.currentPage >=lastPage){
			startPage = this.currentPage - firstPage + 1;
		}else if((this.currentPage - firstPage) > 0 && this.totalPage - this.currentPage < lastPage){
			startPage = this.totalPage - this.groupPageCount + 1;
		}
		
		//重新计算防止出现为负
		if(startPage < 1){
			startPage = 1;
		}
		
		//计算结束页
		if(this.totalPage - this.currentPage >=lastPage && startPage == 1 && this.totalPage >=this.groupPageCount){
			endPage = this.groupPageCount;
		}else if(this.totalPage - this.currentPage >=lastPage && this.totalPage >=this.groupPageCount){
			endPage = this.currentPage + lastPage;
		}else{
			endPage = this.totalPage;
		}
		
		//从起始到结束添加集合
		for (int i = startPage; i <= endPage; i++) {
			groupPage.add(i);
		}
		
		return groupPage;
	}
	

	
}
